package net.visibleblue.android.zxing.result;

import java.lang.reflect.Array;
import java.util.ArrayList;

import com.google.zxing.Result;

import android.content.Intent;

public final class SeriesParsedResult extends ParsedReaderResult {

	private final ParsedReaderResult[] _series;
	
	SeriesParsedResult(Object seriesObjects)
	{
		super(ParsedReaderResultType.SERIES);
		_series = getSeries(seriesObjects);
	}
	
	private static ParsedReaderResult[] getSeries(Object seriesObjects)
	{
		ParsedReaderResult[] resultList = new ParsedReaderResult[Array.getLength(seriesObjects)];
		for(int i=0; i<Array.getLength(seriesObjects); i++)
		{
			Object item = Array.get(seriesObjects, i);
			if(item instanceof String)
			{
				resultList[i] = ParsedReaderResult.parseReaderResult(new Result((String)item, 
						null, null, null));
			}
			else if(item instanceof Intent)
				resultList[i] = new AndroidIntentParsedResult((Intent)item);
			else
			{
				throw new IllegalArgumentException("SeriesParsedResult elements must be" +
						"of type String or IntentInfo, not " + item.getClass().toString());
			}
		}
		return resultList;
	}
	
	public ParsedReaderResult[] getResults()
	{
		return _series;
	}
	
	@Override
	public String getDisplayResult() {
		return Array.getLength(_series) + " element series of results.";
	}

}
